<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<c:set var="message" value="${param['successMessage']}"/>
<c:if test="${message != null}">
    <span><fmt:message key="${message}"/></span>
</c:if>
<c:set var="message" value="${param['errorMessage']}"/>
<c:if test="${message != null}">
    <span class="error"><fmt:message key="${message}"/></span>
</c:if>
<c:if test="${not empty usersList}">
    <form:form id="deleteUser" method="POST">
        <input type="hidden" id="deleteUserId" name="userId"/>
    </form:form>
    <div id="div_add_users">
        <h4>Users list:</h4>
        <table id="users_table">
            <tr>
                <th>First name</th>
                <th>Last name</th>
                <th>Username</th>
                <th>Role</th>
                <th>Action</th>
            </tr>
            <c:forEach items="${usersList}" var="user">
                <tr>
                    <td>${user.firstName}</td>
                    <td>${user.lastName}</td>
                    <td>${user.username}</td>
                    <td>${user.role}</td>
                    <td><a href="#" onclick="deleteUser('${user.id}');">Delete</a></td>
                </tr>
            </c:forEach>
        </table>
    </div>
</c:if>
<br><br>

<form:form id="add_user_form" commandName="user">
    <div id="users">
        <h4>Add new user</h4>
        <table id="add_user_table">
            <tr>
                <td>First name:</td>
                <td><form:input path="firstName"/></td>
                <td><form:errors class="error" path="firstName"/></td>
            </tr>
            <tr>
                <td>Last name:</td>
                <td><form:input path="lastName"/></td>
                <td><form:errors class="error" path="lastName"/></td>
            </tr>
            <tr>
                <td>User role:</td>
                <td><form:select path="role">
                    <form:option value="-" label="--Please Select--"/>
                    <form:options/>
                </form:select></td>
                <td><form:errors class="error" path="role"/></td>
            </tr>
            <tr>
                <td colspan="2" id="td_add_user">
                    <input class="no_border" id="add_user" type="button" value="Add user" onclick="addUser();"/>
                </td>
            </tr>
        </table>
        <p>* The username and password will be generated in the format: lastname_firstname </p>
    </div>
</form:form>